package fct.mpc.utils;

import java.util.ArrayList;
import java.util.Random;

import android.location.Location;

import com.google.android.maps.GeoPoint;

public class HuntingGrounds
{
	protected static final int margin = (int)(0.0001 * 1E6);
	
	protected static ArrayList<HuntingGround> huntingGrounds = new ArrayList<HuntingGround>();
	protected static ArrayList<GeoPoint> snipeLocations = new ArrayList<GeoPoint>();
	
	protected static Random randomizer = new Random();
	
	private HuntingGrounds()
	{
	}
	
	public static ArrayList<GeoPoint> getSnipeLocations()
	{
		for(int i=0; i<huntingGrounds.size(); i++)
		{
			while(!huntingGrounds.get(i).hasSnipe())
			{
				int lat = huntingGrounds.get(i).getLatSouth() + (randomizer.nextInt(1) * huntingGrounds.get(i).getLatitudeSpan());
				int lon = huntingGrounds.get(i).getLonWest() + (randomizer.nextInt(1) * huntingGrounds.get(i).getLongitudeSpan());
			
				if(isInHuntingGround(lat, lon))
				{
					snipeLocations.add(new GeoPoint(lat, lon));
					huntingGrounds.get(i).hasSnipe(true);
					break;
				}
			}
		}
		
		return snipeLocations;
	}
	
	public static void addHuntingGround(String name, int north, int south, int east, int west)
	{
		huntingGrounds.add(new HuntingGround(name, north, east, south, west));
	}
	
	public static void addHuntingGround(String name, double north, double south, double east, double west)
	{
		huntingGrounds.add(new HuntingGround(name, north, east, south, west));
	}
	
	public static void removeHuntingGround(String name)
	{
		for(int i=0; i<huntingGrounds.size(); i++)
		{
			if(huntingGrounds.get(i).getHuntingGroundName().equalsIgnoreCase(name))
			{
				huntingGrounds.remove(i);
				break;
			}
		}
	}
	
//	public static boolean isInHuntingGround(GeoPoint point)
//	{
//		for(int i=0; i<huntingGrounds.size(); i++)
//		{
//			if(huntingGrounds.get(i).contains(point))
//				return true;
//		}
//		
//		return false;
//	}
//	
	public static boolean isInHuntingGround(int latitude, int longitude)
	{
		for(int i=0; i<huntingGrounds.size(); i++)
		{
			if(huntingGrounds.get(i).contains(latitude, longitude))
				return true;
		}
		
		return false;
	}
	
	public static boolean nearSnipe(Location myLocation)
	{
		int myLat = (int)(myLocation.getLatitude() * 1E6);
		int myLon = (int)(myLocation.getLongitude() * 1E6);
		
		ArrayList<GeoPoint> snipes = HuntingGrounds.getSnipeLocations();        		
		for(int i=0; i<snipes.size(); i++)
		{
			int snpLat = snipes.get(i).getLatitudeE6();
			int snpLon = snipes.get(i).getLongitudeE6();
			
			if((myLat >= snpLat - margin && myLat <= snpLat + margin) && (myLon >= snpLon - margin && myLon <= snpLon + margin))
				return true;
		}
		
		return false;
	}
	
//	public static boolean isInHuntingGround(double latitude, double longitude)
//	{
//		for(int i=0; i<huntingGrounds.size(); i++)
//		{
//			if(huntingGrounds.get(i).contains((int)(latitude * 1E6), (int)(longitude * 1E6)))
//				return true;
//		}
//		
//		return false;
//	}
	
	private static class HuntingGround
	{
		protected final String huntingGroundName;
		
		protected final int latNorth; 
	    protected final int latSouth; 
	    protected final int lonEast; 
	    protected final int lonWest;
	    
	    protected boolean hasSnipe = false;
	    
	    public HuntingGround(String name, int north, int east, int south, int west)
	    {
	    	this.huntingGroundName = name;
	    	
	        this.latNorth = north; 
	        this.lonEast = east; 
	        this.latSouth = south; 
	        this.lonWest = west; 
	    } 
	  
	    public HuntingGround(String name, double north, double east, double south, double west)
	    { 
	    	this.huntingGroundName = name;
	    	
	        this.latNorth = (int)(north * 1E6); 
	        this.lonEast = (int)(east * 1E6); 
	        this.latSouth = (int)(south * 1E6); 
	        this.lonWest = (int)(west * 1E6); 
	    }

	    public String getHuntingGroundName()
	    {
	    	return this.huntingGroundName;
	    }
	    
//	    public int getLatNorth()
//	    { 
//	        return this.latNorth; 
//	    }
	    
	    public int getLatSouth()
	    { 
	        return this.latSouth; 
	    }
	    
//	    public int getLonEast()
//	    { 
//	        return this.lonEast; 
//	    }
	    
	    public int getLonWest()
	    { 
	        return this.lonWest; 
	    }
	    
	    public boolean hasSnipe()
	    {
	    	return this.hasSnipe;
	    }
	    
	    public void hasSnipe(boolean has)
	    {
	    	this.hasSnipe = has;
	    }
	    
	    public int getLatitudeSpan()
	    { 
	        return Math.abs(this.latNorth - this.latSouth); 
	    }
	    
	    public int getLongitudeSpan()
	    { 
	        return Math.abs(this.lonEast - this.lonWest); 
	    }

//	    public boolean contains(GeoPoint point)
//	    { 
//	        return contains(point.getLatitudeE6(), point.getLongitudeE6()); 
//	    } 
	  
	    public boolean contains(int latitude, int longitude)
	    { 
	        return (latitude <= this.latNorth && latitude >= this.latSouth && longitude <= this.lonEast && longitude >= this.lonWest); 
	    }
	}
}